/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-04-17     Linda        the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "dev_gt968.h"

#define MAX_TOUCH_POINT 5
#define TOUCH_POINT_BUFFER_SIZE 8

#define GT968_TS_ADDR               (0x14)

#define GT968_CTRL_REG 	    (0x4080) // 0:读坐标状态 1:差值原始值 2:软件复位 3:基准更新 4:基准校准 5:关屏(发送其它无效)
#define GT968_CFGS_REG 	    (0x4780)  
#define GT968_CHECK_REG 	(0xFF80)  

#define GT968_PID_REG 		(0x4081)   
#define GT968_STATUS_REG 	(0x4E81)  
#define GT968_TP1_REG 		(0x5081)  
#define GT968_TP2_REG 		(0x5881)   
#define GT968_TP3_REG 		(0x6081) 
#define GT968_TP4_REG 		(0x6881)   
#define GT968_TP5_REG 		(0x7081)   

#define GT968_CONF_LEN      186     // 外设的需要配置的寄存器长度
/*
rt_uint8_t g_config9147[GT9147_CONF_LEN]={
                0x62,0xe0,0x01,0x20,0x03,0x05,0x34,0xc0,0x01,0x08,
                0x28,0x0f,0x50,0x32,0x03,0x05,0x00,0x00,0x00,0x00,
                0x00,0x00,0x06,0x16,0x16,0x1f,0x14,0x89,0x28,0x0a,
                0x17,0x15,0x31,0x0d,0x00,0x00,0x08,0x22,0x04,0x11,
                0x00,0x00,0x00,0x00,0x00,0x03,0x82,0x08,0x08,0x00,
                0x00,0x0f,0x2c,0x94,0xc5,0x02,0x07,0x00,0x00,0x04,
                0x9d,0x10,0x00,0x84,0x14,0x00,0x70,0x19,0x00,0x5f,
                0x20,0x00,0x55,0x27,0x00,0x54,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
                0x00,0x00,0x1a,0x18,0x16,0x14,0x12,0x10,0x0e,0x0c,
                0x0a,0x08,0x00,0x00,0x00,0x00,0x1f,0xff,0xff,0xff,
                0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
                0xff,0x00,0x00,0x02,0x04,0x05,0x06,0x08,0x0a,0x0c,
                0x0e,0x1d,0x1e,0x1f,0x20,0x22,0x24,0x28,0x29,0xff,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
                0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf6,
                0xff,0xff,0xff,0x00
    };
*/
rt_uint8_t g_gt968_config[GT968_CONF_LEN]={
                0x00,0x20,0x03,0xE0,0x01,0x01,0x3C,0x00,0x01,0x08,
                0x19,0x05,0x4A,0x3A,0x03,0x0F,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x18,0x1A,0x1E,0x14,0x89,0x2A,0x09,
                0x2F,0x2B,0x88,0x13,0x00,0x00,0x01,0xB8,0x03,0x2D,
                0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x10,0x12,
                0x14,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0F,0x10,
                0x12,0x16,0x18,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
                0x24,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                0x00,0x00,0x33,0x00,
    };
// 存一下读取的产品id
static char g_gt9xxx_pid[4] = {0};//GT911 or GT9147


rt_dev_gt_touch_attr_t gt968_attr = {
    .chip_id = "GT968",
    .i2c_addr = GT968_TS_ADDR,
    .reg_ctrl = GT968_CTRL_REG,
    .reg_cfg = GT968_CFGS_REG,
    .reg_chk = GT968_CHECK_REG,
    .reg_product_id = GT968_PID_REG,
    .reg_status = GT968_STATUS_REG,
    .reg_tp = {GT968_TP1_REG,GT968_TP2_REG,GT968_TP3_REG,GT968_TP4_REG,GT968_TP5_REG},
    .cfg_size = GT968_CONF_LEN,
    .cfg_table = g_gt968_config,
};

rt_dev_gt_touch_attr_t *rt_get_tp_attr(void)
{
    return &gt968_attr;
}